
## libraries 

library(ggplot2)  
library(scales)  
library(ggrepel)
library(WDI)
library(data.table)
library(plyr)
library(readstata13)  
library(gridExtra)
library(gnumeric)
library(DataCombine) 
library(cowplot)
library(haven)
library(lme4)
library(car)
library(arm)
library(dplyr)
library(MuMIn)
library(lmerTest)
library(estimatr)
library(stargazer)


## plot format 

theme_article <- function(base_size=11, font="Times New Roman"){
  
  txt <- element_text(size = base_size+2, colour = "black", face = "plain", family=font)
  bold_txt <- element_text(size = base_size+2, colour = "black", face = "bold", family=font)
  
  theme_bw(base_size = base_size, base_family = font)+
    theme(
      legend.key = element_blank(), 
      strip.background = element_blank(), 
      text = txt, 
      plot.title = txt, 
      
      axis.title = txt, 
      axis.text = txt, 
      
      legend.title = bold_txt, 
      legend.text = txt ) +
    
    theme(
      panel.grid.major.x = element_blank() , 
      panel.grid.minor.x = element_blank(), 
      panel.grid.major.y = element_line( size=.5, color="#CCCCCC"), panel.grid.minor.y = element_line( size=.5, color="#DDDDDD"),
      
      axis.line.y = element_line(colour = "#CCCCCC", size = 0.3),
      axis.line.x = element_line(colour = "#CCCCCC", size = 0.3),
      #### remove Tick marks
      axis.ticks=element_blank(),
      
      legend.position = "none", 
            plot.background = element_rect(fill = "white",colour = "white",size = 0.5)
    )
}




save.plot <- function(name = "test.png", width=6, height= 6, usewd=F){
    wd <- getwd() 
    if(usewd == F) {
    setwd("~/R") }
  else {setwd(wd)}
    ggsave(name, width= width, height= height)
  dev.off()
  setwd(wd) 
}



## load the combined dataset 

my.data <- read.csv("combined_survey_tweets_february_11.csv")

## check variable codings 

table(my.data$pastvote_ge_2019)
table(my.data$newspaper)
table(my.data$profile_gender)
table(my.data$profile_education_level)
table(my.data$profile_gross_household)
table(as.numeric(my.data$profile_gross_household))
table(my.data$pastvote_EURef)
table(my.data$profile_GOR)
table(my.data$week)
table(my.data$lastvote)


## some variables need recoding as factors and releveling

my.data$lastvote <- factor(my.data$lastvote)
my.data$lastvote <- relevel(my.data$lastvote, "Did not vote")

## check base category 
levels(my.data$lastvote)

# simplify region variable 

my.data$region <- "" 
my.data$region[my.data$profile_GOR=="North_West"] <- "North"
my.data$region[my.data$profile_GOR=="Yorkshire_and_the_Humber" ] <- "North"
my.data$region[my.data$profile_GOR=="North_East"] <- "North"
my.data$region[my.data$profile_GOR=="East_Midlands"] <- "Midlands"
my.data$region[my.data$profile_GOR=="West_Midlands"] <- "Midlands"
my.data$region[my.data$profile_GOR=="East_of_England"] <- "South"
my.data$region[my.data$profile_GOR=="London"] <- "London"
my.data$region[my.data$profile_GOR=="South_East"] <- "South"
my.data$region[my.data$profile_GOR=="South_West"] <- "South"
my.data$region[my.data$profile_GOR=="Wales"] <- "Wales"
my.data$region[my.data$profile_GOR=="Scotland"] <- "Scotland"

my.data$region <- factor(my.data$region)

levels(my.data$region)


# generate continuous income variable 

table(my.data$profile_gross_household)

my.data$income <- NA
my.data$income[my.data$profile_gross_household=="under £5,000 per year"] <- 2.5
my.data$income[my.data$profile_gross_household=="£5,000 to £9,999 per year"] <- 7.5
my.data$income[my.data$profile_gross_household=="£10,000 to £14,999 per year"] <- 12.5
my.data$income[my.data$profile_gross_household=="£15,000 to £19,999 per year"] <- 17.5
my.data$income[my.data$profile_gross_household=="£20,000 to £24,999 per year"] <- 22.5
my.data$income[my.data$profile_gross_household=="£25,000 to £29,999 per year"] <- 27.5
my.data$income[my.data$profile_gross_household=="£30,000 to £34,999 per year"] <- 32.5
my.data$income[my.data$profile_gross_household=="£35,000 to £39,999 per year"] <- 37.5

my.data$income[my.data$profile_gross_household=="£40,000 to £44,999 per year"] <- 42.5
my.data$income[my.data$profile_gross_household=="£45,000 to £49,999 per year"] <- 47.5
my.data$income[my.data$profile_gross_household=="£50,000 to £59,999 per year"] <- 55
my.data$income[my.data$profile_gross_household=="£60,000 to £69,999 per year"] <- 65
my.data$income[my.data$profile_gross_household=="£70,000 to £99,999 per year"] <- 85

my.data$income[my.data$profile_gross_household=="£100,000 to £149,999 per year"] <- 125
my.data$income[my.data$profile_gross_household=="£150,000 and over"] <- 180
my.data$income[my.data$profile_gross_household=="Don't know"] <- NA
my.data$income[my.data$profile_gross_household=="Prefer_not_to_answer"] <- NA

table(my.data$income)

## age square 

my.data$age_square <- my.data$age^2

# survey round 

my.data$round <- as.numeric(my.data$week)

## binary coding of demsat

my.data$DEM_SAT <- NA 
my.data$DEM_SAT[my.data$CAM_demtrack_satisfied=="Satisfied"] <- 1
my.data$DEM_SAT[my.data$CAM_demtrack_satisfied=="Not Satisfied"] <- 0

table(my.data$CAM_demtrack_satisfied)


## simple categories for past vote 

table(my.data$pastvote_ge_2019)

my.data$lastvote <- ""
my.data$lastvote[as.character(my.data$pastvote_ge_2019)=="0"] <- ""
my.data$lastvote[as.character(my.data$pastvote_ge_2019)=="Did not vote"] <- ""
my.data$lastvote[as.character(my.data$pastvote_ge_2019)=="Conservatives"] <- "Conservative"
my.data$lastvote[as.character(my.data$pastvote_ge_2019)=="Labour"] <- "Labour"
my.data$lastvote[as.character(my.data$pastvote_ge_2019)=="Liberal Democrats"] <- "LiberalDemocrat"


## dummy leaver/remainer vars

# version coding NAs to 0. Also have with those as NA.
my.data$brexiteer <- 0
my.data$brexiteer[my.data$pastvote_EURef=="Voted Leave"] <- 1

my.data$leaver <- 0
my.data$leaver[my.data$pastvote_EURef=="Voted Leave"] <- 1


## simple left/right variables by newspaper readership 

my.data$leftpaper <- ""
my.data$leftpaper[my.data$newspaper=="guardian"] <- "Left Paper"
my.data$leftpaper[my.data$newspaper=="daily_mirror"] <- "Left Paper"
my.data$leftpaper[my.data$newspaper=="independent"] <- "Left Paper"

my.data$leftpaper[my.data$newspaper=="sun"] <- "Right Paper"
my.data$leftpaper[my.data$newspaper=="daily_mail"] <- "Right Paper"
my.data$leftpaper[my.data$newspaper=="times"] <- "Right Paper"
my.data$leftpaper[my.data$newspaper=="telegraph"] <- "Right Paper"


my.data$remainer <- NA 
my.data$remainer[my.data$pastvote_EURef=="Voted Remain"] <- 1 
my.data$remainer[my.data$pastvote_EURef=="Voted Leave"] <- 0 
my.data$remainer[my.data$pastvote_EURef=="Did not vote"] <- 0 


## check correspondence 
table(my.data$remainer, my.data$pastvote_EURef)

## check variable factor levels 

my.data$lastvote <- factor(my.data$lastvote)
levels(my.data$lastvote)

my.data$newspaper <- factor(my.data$newspaper)
levels(my.data$newspaper)

my.data$region <- factor(my.data$region)
levels(my.data$region)

my.data$profile_gender <-  factor(my.data$profile_gender)
my.data$profile_education_level <- factor(my.data$profile_education_level)



#### basic descriptive trends for a weekly series


weekly.data <-  ddply( my.data, .(week),
                       function(x) data.frame(
                         Neutral.Topics = mean(x$Neutral.Topics, na.rm=T),
                         Negative.Topics=mean(x$Negative.Topics, na.rm=T),
                         Negative.Topics.RT=mean(x$Negative.Topics.RT, na.rm=T),
                         DEM_SAT=weighted.mean(x$DEM_SAT, w=x$weight, na.rm=T),
                         attention=weighted.mean(x$political_attention, w=x$weight, na.rm=T)
                       )  ) 

## set date to first week + weekly round of collection 
weekly.data$date <- as.Date("2019-11-22") + ((weekly.data$week-1)*7)


attention_series <- 
    ggplot(weekly.data, aes(x=as.Date(date)+5, y=rolling.lastx(attention-weekly.data$attention[6],12)  ))  + 
  annotate(geom="rect", xmin=as.Date("2019-02-01"), xmax=as.Date("2022-03-04"), ymin=-Inf, ymax=Inf, fill="white", alpha=1) + 
  annotate(geom="rect", xmin=as.Date("2020-02-01"), xmax=as.Date("2020-02-04"), ymin=-Inf, ymax=Inf, fill="black", alpha=0.1) + 
  coord_cartesian(xlim=c(as.Date("2020-01-01"), as.Date("2022-01-01"))) +
  scale_y_continuous(breaks=c(-0.2,-0.1, 0, 0.1), labels=c("-20%","-10%","0%","+10%")) + 
  annotate(geom="rect", xmin=as.Date("2020-03-26"), xmax=as.Date("2020-07-04"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  annotate(geom="rect", xmin=as.Date("2020-10-31"), xmax=as.Date("2021-04-12"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  geom_area() +
  theme_article() +
  xlab("") + ylab("Attention to Politics")

# check 
attention_series


swd_series <- 
  ggplot(weekly.data, aes(x=as.Date(date)+5, y=rolling.lastx(1-DEM_SAT-0.55,10)  ))  + 
  annotate(geom="rect", xmin=as.Date("2019-02-01"), xmax=as.Date("2022-03-04"), ymin=-Inf, ymax=Inf, fill="white", alpha=1) + 
  annotate(geom="rect", xmin=as.Date("2020-02-01"), xmax=as.Date("2020-02-04"), ymin=-Inf, ymax=Inf, fill="black", alpha=0.1) + 
  coord_cartesian(xlim=c(as.Date("2020-01-01"), as.Date("2022-01-01"))) +
  scale_y_continuous(breaks=c(-0.15, -0.1, -0.05, 0, 0.05), labels=c("-15%","-10%","-5%","0%","+5%")) + 
  annotate(geom="rect", xmin=as.Date("2020-03-26"), xmax=as.Date("2020-07-04"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  annotate(geom="rect", xmin=as.Date("2020-10-31"), xmax=as.Date("2021-04-12"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  geom_area() +
  theme_article() +
  xlab("") + ylab("Dissatisfaction with Democracy")

swd_series 


negative_topic_series <- 
  ggplot(data=weekly.data, aes(x=date+5, y=rolling.lastx(Negative.Topics-0.41,10) )) + 
  annotate(geom="rect", xmin=as.Date("2019-02-01"), xmax=as.Date("2022-03-04"), ymin=-Inf, ymax=Inf, fill="white", alpha=1) + 
  coord_cartesian(xlim=c(as.Date("2020-01-01"), as.Date("2022-01-01")), ylim=c(-0.08, 0.08)) +
  annotate(geom="rect", xmin=as.Date("2020-02-01"), xmax=as.Date("2020-02-04"), ymin=-Inf, ymax=Inf, fill="black", alpha=0.1) + 
  annotate(geom="rect", xmin=as.Date("2020-03-26"), xmax=as.Date("2020-07-04"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  annotate(geom="rect", xmin=as.Date("2020-10-31"), xmax=as.Date("2021-04-12"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  geom_area() +
  scale_y_continuous(breaks=c(-0.05, 0, 0.05), labels=c("-5%","0%","+5%")) + 
  annotate(geom="text", label="First \nCOVID-19\nCase in\nEurope", x=as.Date("2020-01-25"), hjust=1, vjust=1,
           y=0.075, family="PTSerif",alpha=0.35, size=3.4) +
  annotate(geom="text", label=" First \nLockdown", x=as.Date("2020-05-10"), y=0.05, family="PTSerif",alpha=0.35) +
  annotate(geom="text", label=" Second \nLockdown", x=as.Date("2021-01-20"), y=0.05, family="PTSerif",alpha=0.35) +
  theme_article() +
  xlab("") + ylab("Polarizing Media Content")



neutral_topic_series <- 
  ggplot(data=weekly.data, aes(x=date, y=rolling.lastx(1-Neutral.Topics-0.55,10) )) + 
  annotate(geom="rect", xmin=as.Date("2019-02-01"), xmax=as.Date("2022-02-04"), ymin=-Inf, ymax=Inf, fill="white", alpha=1) + 
  coord_cartesian(xlim=c(as.Date("2020-01-01"), as.Date("2022-01-01"))) +
  annotate(geom="rect", xmin=as.Date("2020-02-01"), xmax=as.Date("2020-02-04"), ymin=-Inf, ymax=Inf, fill="black", alpha=0.05) + 
  annotate(geom="rect", xmin=as.Date("2020-03-26"), xmax=as.Date("2020-06-01"), ymin=-Inf, ymax=Inf, fill="red", alpha=0.05) + 
  annotate(geom="rect", xmin=as.Date("2021-01-01"), xmax=as.Date("2021-04-15"), ymin=-Inf, ymax=Inf, fill="red", alpha=0.05) + 
  geom_area() +
  theme_article() +
  xlab("") + ylab("Neutral Media Topic Coverage") 

library(cowplot)

plot_grid(negative_topic_series, attention_series, swd_series, align = "v", nrow = 3, rel_heights = c(1/3, 1/3, 1/3))


#### re-doing this for the reviewer- changing the polarity of the y axes as requested 
## Also re-positioning dates to exact weekly midpoints, for precise accuracy of presentation. 

negative_topic_series_inv <- 
  ggplot(data=weekly.data, aes(x=date+5, y=-rolling.lastx(Negative.Topics-0.41,10) )) + 
  annotate(geom="rect", xmin=as.Date("2019-02-01"), xmax=as.Date("2022-03-04"), ymin=-Inf, ymax=Inf, fill="white", alpha=1) + 
  coord_cartesian(xlim=c(as.Date("2020-01-01"), as.Date("2022-01-01")), ylim=c(-0.08, 0.08)) +
  annotate(geom="rect", xmin=as.Date("2020-02-01"), xmax=as.Date("2020-02-04"), ymin=-Inf, ymax=Inf, fill="black", alpha=0.1) + 
  annotate(geom="rect", xmin=as.Date("2020-03-26"), xmax=as.Date("2020-07-04"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  annotate(geom="rect", xmin=as.Date("2020-10-31"), xmax=as.Date("2021-04-12"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  geom_area() +
  scale_y_continuous(breaks=c(-0.05, 0, 0.05), labels=c("-5%","0%","+5%")) + 
  annotate(geom="text", label="First \nCOVID-19\nCase in\nEurope", x=as.Date("2020-01-25"), hjust=1, vjust=1,
           y=-0.01, family="PTSerif",alpha=0.35, size=3.4) +
  annotate(geom="text", label=" First \nLockdown", x=as.Date("2020-05-10"), y=-0.05, family="PTSerif",alpha=0.35) +
  annotate(geom="text", label=" Second \nLockdown", x=as.Date("2021-01-20"), y=-0.05, family="PTSerif",alpha=0.35) +
  theme_article(base_size=14) +
  xlab("") 

attention_series_inv <- 
  ggplot(weekly.data, aes(x=as.Date(date)+5, y= -rolling.lastx(attention-8.35,10)  ))  + 
  annotate(geom="rect", xmin=as.Date("2019-02-01"), xmax=as.Date("2022-03-04"), ymin=-Inf, ymax=Inf, fill="white", alpha=1) + 
  annotate(geom="rect", xmin=as.Date("2020-02-01"), xmax=as.Date("2020-02-04"), ymin=-Inf, ymax=Inf, fill="black", alpha=0.1) + 
  coord_cartesian(xlim=c(as.Date("2020-01-01"), as.Date("2022-01-01"))) +
  scale_y_continuous(breaks=c(-0.1, 0, 0.1, 0.2), labels=c("-10%","0%","+10%", "+20%")) + 
  annotate(geom="rect", xmin=as.Date("2020-03-26"), xmax=as.Date("2020-07-04"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  annotate(geom="rect", xmin=as.Date("2020-10-31"), xmax=as.Date("2021-04-12"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  geom_area() +
  theme_article(base_size = 14) +
  xlab("") + ylab("Pay No Attention\n to Politics")

swd_series_inv <- 
  ggplot(weekly.data, aes(x=as.Date(date)+5, y= -rolling.lastx(1-DEM_SAT-0.55,10)  ))  + 
  annotate(geom="rect", xmin=as.Date("2019-02-01"), xmax=as.Date("2022-03-04"), ymin=-Inf, ymax=Inf, fill="white", alpha=1) + 
  annotate(geom="rect", xmin=as.Date("2020-02-01"), xmax=as.Date("2020-02-04"), ymin=-Inf, ymax=Inf, fill="black", alpha=0.1) + 
  coord_cartesian(xlim=c(as.Date("2020-01-01"), as.Date("2022-01-01"))) +
  scale_y_continuous(breaks=c( -0.05, 0, 0.05, 0.1, 0.15), labels=c("-5%","0%","+5%", "+10%", "+15%")) + 
  annotate(geom="rect", xmin=as.Date("2020-03-26"), xmax=as.Date("2020-07-04"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  annotate(geom="rect", xmin=as.Date("2020-10-31"), xmax=as.Date("2021-04-12"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  geom_area() +
  theme_article(base_size=14) +
  xlab("") + ylab("Satisfaction \nwith Democracy")

plot_grid(negative_topic_series_inv, attention_series_inv, swd_series_inv, align = "v", nrow = 3, rel_heights = c(1/3, 1/3, 1/3))


## Once more, this time exactly as suggested (swd as positive polarity, others negative)
## Also re-positioning dates to exact weekly midpoints, for accuracy of presentation. 

attention_series2 <- 
  ggplot(weekly.data, aes(x=as.Date(date)+5, y=rolling.lastx(attention-8.35,10)  ))  + 
  annotate(geom="rect", xmin=as.Date("2019-02-01"), xmax=as.Date("2022-03-04"), ymin=-Inf, ymax=Inf, fill="white", alpha=1) + 
  annotate(geom="rect", xmin=as.Date("2020-02-01"), xmax=as.Date("2020-02-04"), ymin=-Inf, ymax=Inf, fill="black", alpha=0.1) + 
  coord_cartesian(xlim=c(as.Date("2020-01-01"), as.Date("2022-01-01"))) +
  scale_y_continuous(breaks=c(-0.2,-0.1, 0, 0.1), labels=c("-20%","-10%","0%","+10%")) + 
  annotate(geom="rect", xmin=as.Date("2020-03-26"), xmax=as.Date("2020-07-04"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  annotate(geom="rect", xmin=as.Date("2020-10-31"), xmax=as.Date("2021-04-12"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  geom_area() +
  theme_article(base_size=14) +
  xlab("") + ylab("Attention \nto Politics")

negative_topic_series2 <- 
  ggplot(data=weekly.data, aes(x=date+5, y=rolling.lastx(Negative.Topics-0.41,10) )) + 
  annotate(geom="rect", xmin=as.Date("2019-02-01"), xmax=as.Date("2022-03-04"), ymin=-Inf, ymax=Inf, fill="white", alpha=1) + 
  coord_cartesian(xlim=c(as.Date("2020-01-01"), as.Date("2022-01-01")), ylim=c(-0.08, 0.08)) +
  annotate(geom="rect", xmin=as.Date("2020-02-01"), xmax=as.Date("2020-02-04"), ymin=-Inf, ymax=Inf, fill="black", alpha=0.1) + 
  annotate(geom="rect", xmin=as.Date("2020-03-26"), xmax=as.Date("2020-07-04"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  annotate(geom="rect", xmin=as.Date("2020-10-31"), xmax=as.Date("2021-04-12"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  geom_area() +
  scale_y_continuous(breaks=c(-0.05, 0, 0.05), labels=c("-5%","0%","+5%")) + 
  annotate(geom="text", label="First \nCOVID-19\nCase in\nEurope", x=as.Date("2020-01-25"), hjust=1, vjust=1,
           y=0.075, family="PTSerif",alpha=0.35, size=3.4) +
  annotate(geom="text", label=" First \nLockdown", x=as.Date("2020-05-10"), y=0.05, family="PTSerif",alpha=0.35) +
  annotate(geom="text", label=" Second \nLockdown", x=as.Date("2021-01-20"), y=0.05, family="PTSerif",alpha=0.35) +
  theme_article(base_size=14) +
  xlab("") + ylab("Polarizing \nMedia Content")


plot_grid(negative_topic_series2, attention_series2, swd_series_inv, align = "v", nrow = 3, rel_heights = c(1/3, 1/3, 1/3))


## further descriptives: 
## plotting left-right partisan gap over key variables. 


weekly.data.left <-  ddply( subset(my.data,leftpaper=="Left Paper")  , .(week),
                            function(x) data.frame(
                              Neutral.Topics = mean(x$Neutral.Topics, na.rm=T),
                              Negative.Topics=mean(x$Negative.Topics, na.rm=T),
                              Negative.Topics.RT=mean(x$Negative.Topics.RT, na.rm=T),
                              DEM_SAT=weighted.mean(x$DEM_SAT, w=x$weight, na.rm=T),
                              attention=weighted.mean(x$political_attention, w=x$weight, na.rm=T)
                            )  ) 

weekly.data.left$date <- as.Date("2019-11-22") + ((weekly.data.left$week-1)*7)


weekly.data.right <-  ddply( subset(my.data,leftpaper=="Right Paper")  , .(week),
                             function(x) data.frame(
                               Neutral.Topics = mean(x$Neutral.Topics, na.rm=T),
                               Negative.Topics=mean(x$Negative.Topics, na.rm=T),
                               Negative.Topics.RT=mean(x$Negative.Topics.RT, na.rm=T),
                               DEM_SAT=weighted.mean(x$DEM_SAT, w=x$weight, na.rm=T),
                               attention=weighted.mean(x$political_attention, w=x$weight, na.rm=T)
                             )  ) 

weekly.data.right$date <- as.Date("2019-11-22") + ((weekly.data.right$week-1)*7)

## quick eyeball check - frames align exactly by week
cbind(weekly.data.right$week, weekly.data.left$week)

# then we can just do this:
weekly.data.left.minus.right <- weekly.data.left - weekly.data.right
weekly.data.left.minus.right$week <- weekly.data.left$week # these should have aligned already 
weekly.data.left.minus.right$date <- as.Date("2019-11-22") + ((weekly.data.left.minus.right$week-1)*7)



leftright_attention_gap <- 
  ggplot(subset(weekly.data.left.minus.right, date>as.Date("2020-02-08")), aes(x=as.Date(date), y=rolling.lastx(attention,10)  ))  + 
  annotate(geom="rect", xmin=as.Date("2019-02-01"), xmax=as.Date("2022-02-04"), ymin=-Inf, ymax=Inf, fill="white", alpha=1) + 
  annotate(geom="rect", xmin=as.Date("2020-02-01"), xmax=as.Date("2020-02-04"), ymin=-Inf, ymax=Inf, fill="black", alpha=0.1) + 
  coord_cartesian(xlim=c(as.Date("2020-01-01"), as.Date("2022-01-01")), ylim=c(0.45,.8)) +
  annotate(geom="rect", xmin=as.Date("2020-03-26"), xmax=as.Date("2020-07-04"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  annotate(geom="rect", xmin=as.Date("2020-10-31"), xmax=as.Date("2021-04-12"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  geom_line(color="black", size=1.5) + 
  geom_line(color="#888888", size=1) + 
  theme_article(base_size=14) +
  xlab("") + ylab("Attention to Politics,\n Left-Right Gap\n ")

leftright_demsat_gap <- 
  ggplot(subset(weekly.data.left.minus.right, date>as.Date("2020-02-01")), aes(x=as.Date(date), y=rolling.lastx(-DEM_SAT,10)  ))  + 
  annotate(geom="rect", xmin=as.Date("2019-02-01"), xmax=as.Date("2022-02-04"), ymin=-Inf, ymax=Inf, fill="white", alpha=1) + 
  coord_cartesian(xlim=c(as.Date("2020-01-01"), as.Date("2022-01-01"))) +
  annotate(geom="rect", xmin=as.Date("2020-02-01"), xmax=as.Date("2020-02-04"), ymin=-Inf, ymax=Inf, fill="black", alpha=0.1) + 
  annotate(geom="rect", xmin=as.Date("2020-03-26"), xmax=as.Date("2020-07-04"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  annotate(geom="rect", xmin=as.Date("2020-10-31"), xmax=as.Date("2021-04-12"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  geom_line(color="black", size=1.5) + 
  geom_line(color="#888888", size=1) + 
  theme_article(base_size=14) +
  scale_y_continuous(labels=function(x) paste0(x*100,"%"), breaks=c(.42,0.4,0.38,.36,0.34,0.32)) +
  xlab("") + ylab("Satisfaction with Democracy,\n Left-Right Gap\n ")


gapchart_leftright <- 
  ggplot(data=subset(gap, date > as.Date("2020-02-01")), aes(x=date, y=rolling.lastx(gap.negative,7) )) + 
  annotate(geom="rect", xmin=as.Date("2019-02-01"), xmax=as.Date("2022-02-04"), ymin=-Inf, ymax=Inf, fill="white", alpha=1) + 
  annotate(geom="rect", xmin=as.Date("2020-02-01"), xmax=as.Date("2020-02-04"), ymin=-Inf, ymax=Inf, fill="black", alpha=0.1) + 
  coord_cartesian(xlim=c(as.Date("2020-01-01"), as.Date("2022-01-01"))) + 
  annotate(geom="rect", xmin=as.Date("2020-03-26"), xmax=as.Date("2020-07-04"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  annotate(geom="rect", xmin=as.Date("2020-10-31"), xmax=as.Date("2021-04-12"), ymin=-Inf, ymax=Inf, fill="grey", alpha=0.35) + 
  geom_line(color="black", size=1.5) + 
  geom_line(color="#888888", size=1) + 
  scale_y_continuous(breaks = c(-2,0,2,4), labels = c("-2%", "0%", "+2%", "+4%") ) + 
  annotate(geom="text", label=" First\nCOVID-19\nCase in\nEurope", x=as.Date("2020-01-25"), hjust=1, vjust=1,
           y=3, family="PTSerif",alpha=0.35, size=3.4) +
  annotate(geom="text", label=" First \nLockdown", x=as.Date("2020-05-10"), y=-2, family="PTSerif",alpha=0.35) +
  annotate(geom="text", label=" Second \nLockdown", x=as.Date("2021-01-20"), y=-2, family="PTSerif",alpha=0.35) +
  theme_article(base_size=14) + xlab("") + ylab("Polarizing Content,\n Left-Right Gap\n ")


plot_grid(gapchart_leftright, leftright_attention_gap, leftright_demsat_gap, align = "v", nrow = 3, rel_heights = c(1/3, 1/3, 1/3))





######## MLM models

## this will take a while to estimate
mlm1 <- lmer(data=my.data, DEM_SAT ~ age + age_square + as.factor(profile_gender) + factor(region) + 
               profile_education_level    + income + political_attention + 
               ( 1 + factor(leftpaper) + factor(lastvote) + leaver | round )  )

summary(mlm1)


foo <- ranef(mlm1)$round
foo <- adf(foo)

foo$round <- row.names(foo)
foo$round <- n(foo$round)

foo$date <- as.Date("2019-11-22") + ((foo$round-1)*7)

# make variable names easier to call
names(foo) <- c("intercept","LeftPaper", "RightPaper",
                "Conservative","Labour", "LibDem","Leaver", "round","date")


library(zoo)

## display partisan slopes 

ggplot(data=foo, aes(x=as.Date(date), y=rollmean(Labour,13,NA,"right")   )) + 
  geom_smooth(method="lm",se=F, color="black", size=0.75, linetype="dashed") +
  geom_line(color="black", alpha=1, size=3) +
  geom_line(data=subset(foo, date>as.Date("2020-03-15") ), aes(x=as.Date(date), y=rolling.lastx(LeftPaper,8)   ),
            color="darkgrey", alpha=1, size=3) + 
  geom_smooth(data=subset(foo, date>as.Date("2020-03-15") ), aes(x=as.Date(date), y=rolling.lastx(LeftPaper,8) ),
              method="lm",se=F, color="grey", size=0.75, linetype="dashed") +
  theme_article() +
  annotate(geom="text", label="Labour\nVoter", x=as.Date("2022-01-10"), hjust=0, size=3.5, y=-0.095) +
  annotate(geom="text", label="Left-Wing\nReader", x=as.Date("2022-01-10"), hjust=0, size=3.5, y=-0.065, color="darkgrey") +
  xlab("") + 
  scale_y_continuous(labels = function(x) paste0(x*100, "%")) + 
  ylab("Partisanship Slope Coefficient") + 
  theme_article() 


## appendix robustness check - remainer random slopes (since official brexit date of jan 2020)

## request was to estimate using remainer random slope only -
## as alternative hypothesis was that shift among left-wing respondents was specific to moderation of
##  pro-remain sentiment once brexit issue faded in salience, whereas no hypothesised shift among leavers.


mlm_remainer <- lmer(data=my.data, DEM_SAT ~ age + age_square + as.factor(profile_gender) + factor(region) +
                             political_attention  +   ( 1 + factor(leftpaper) + factor(lastvote) + remainer | round )  )


bar <- ranef(mlm_remainer)$round
bar <- adf(bar)

bar$round <- row.names(bar)
bar$round <- n(bar$round)

bar$date <- as.Date("2019-11-22") + ((bar$round-1)*7)

names(bar) <- c("intercept","LeftPaper", "RightPaper", "Conservative","Labour", "LibDem","Remainer", "round","date")

## 90% intervals based on estimated standard errors 
bar$remainer_lb <- ranef(mlm_remainer)$round[,7]-(se.ranef(mlm_remainer)$round[,7]*1.645)
bar$remainer_ub <- ranef(mlm_remainer)$round[,7]+(se.ranef(mlm_remainer)$round[,7]*1.645)

ggplot(bar, aes(x=as.Date(date), y=rolling.lastx(Remainer,3)   )) + 
  geom_ribbon(aes(ymax=rolling.lastx(remainer_ub,3), ymin=rolling.lastx(remainer_lb,3) ),alpha=.05 ) +
  geom_line(size=2, color="black", alpha=0.65) + xlab("") + 
  geom_hline(yintercept=0 , alpha=0.25, size=1.5) +
  geom_smooth(method="lm", size=0.75, linetype="dashed", color="black", se=F) +
  geom_vline(xintercept=as.Date("2020-04-01"), linetype="dotted") +
  scale_y_continuous(breaks=c(-0.2,-0.1,0,0.1,0.2 ), labels=c("-20%","-10%","0%","+10%","+20%")) + 
  coord_cartesian(ylim=c(-0.12,0.12), xlim=c(as.Date("2020-01-01"),as.Date("2022-04-01"))) +
  annotate(geom="text", label="Voted\nRemain", color="black", x=as.Date("2022-01-05"), hjust=0, y=-.021, size=4.2  )   + 
  ylab("Estimated Effect Upon Satisfaction with Democracy") + 
  theme_article(base_size=15) 



## left vs. right-wing paper coverage.

## input file: separates tweets by left/right paper.

Tweets_Sum <- 
  read.csv("Database_Tweets_plot.csv")

Tweets_Sum$created_at.x  <- as.Date(as.character(Tweets_Sum$created_at.x))

ggplot(Tweets_Sum, aes(x=created_at.x, y=percneg, col = newspaper_ideology)) +
  geom_smooth(method="loess", alpha=.1)+
  scale_color_manual(values=c("darkblue","darkred"))+
  labs(title= "", x = "",
       y = "Percentage of Neutral Tweets each week")+
  theme_bw() +
  scale_x_date(labels = date_format("%b %y") ) +
  theme(axis.text.x = element_text(angle = 45, hjust=1))


ggplot( data = subset(Tweets_Sum, newspaper_ideology=="Left Wing Newspaper"), aes(x=created_at.x, y=rolling.lastx(percneg,24) )   ) + 
  geom_line(color="darkred", size=1.5) + 
  geom_line(data = subset(Tweets_Sum, newspaper_ideology=="Right Wing Newspaper"), aes(x=created_at.x, y=rolling.lastx(percneg,24) ), 
            color="darkblue", size=1.5) + 
  theme_classic()


tweets_comparison <- merge(
  subset(Tweets_Sum, newspaper_ideology=="Left Wing Newspaper")[,c("created_at.x","percneg")],
  subset(Tweets_Sum, newspaper_ideology=="Right Wing Newspaper")[,c("created_at.x","percneg")], by=c("created_at.x"))

names(tweets_comparison) <- c("date","leftneg","rightneg")


ggplot( data = tweets_comparison, aes(x=date, y=rolling.lastx( rolling(leftneg-rightneg),21 )-2.1 )   ) + 
  geom_hline(yintercept=0, color="darkgrey") +
  annotate(geom="text", label="Start of  \nfirst lockdown", hjust=0, x=as.Date("2020-04-05"), y=3.125, size=4.75, family="Times New Roman") + 
  geom_vline(xintercept=as.Date("2020-03-26"), color="black", linetype="dotted", size=0.5) + 
  geom_area(fill="darkgrey", size=1.5, alpha=0.5, color="#444444") + 
  theme_article(base_size=15) + 
  coord_cartesian(xlim=c(as.Date("2019-08-31"), as.Date("2022-01-15"))) +
  xlab("") + ylab("Left-Right Negative Topic Coverage Gap")


